package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;


@RestController
@RequestMapping("admin/groupon")
public class GrouponController {

    @Autowired
    GrouponService grouponService;

    @RequestMapping("list")
    //非必然出现的参数可以通过@RequestParam(name = "??",required = false)来解决
    public BaseRespVo list(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit,
                           @RequestParam(name = "goodsId",required = false) Integer goodsId, @RequestParam("sort") String sort,
                           @RequestParam("order") String order) {
        BaseData baseData = grouponService.queryGrouponRules(page,limit,goodsId,sort,order);
        return BaseRespVo.ok(baseData);
    }


    @RequestMapping("create")
    //创建折扣商品时需要确认一次商品是否存在
    public BaseRespVo create(@RequestBody CreateGrouponRulesBO bo) {
        Goods goods = new Goods();
        goods = grouponService.queryGoodsByGoodsId(bo.getGoodsId());
        if(goods == null) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(402);
            baseRespVo.setMsg("参数不对");
            return baseRespVo;
        }
        else {
            CreateGrouponRulesVO vo = grouponService.insertGrouponRules(goods,bo.getDiscount(),
                    bo.getDiscountMember(),bo.getExpireTime());
            return BaseRespVo.ok(vo);
        }
    }

    @RequestMapping("update")
    //如果修改了id，要判断修改后的商品id是否存在，如果不存在，则不允许修改
    public BaseRespVo update(@RequestBody GrouponRules grouponRules) {
        Goods goods = new Goods();
        goods = grouponService.queryGoodsByGoodsId(grouponRules.getGoodsId());
        if(goods == null) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(402);
            baseRespVo.setMsg("参数不对");
            return baseRespVo;
        }
        else {
            grouponRules.setGoodsId(goods.getId());
            grouponRules.setGoodsName(goods.getName());
            grouponRules.setPicUrl(goods.getPicUrl());
            grouponRules.setUpdateTime(new Timestamp(new Date().getTime()));
            grouponService.updateGrouponRules(grouponRules);
            return BaseRespVo.ok();
        }
    }

    @RequestMapping("delete")
    /*下架折扣商品对应的活动：
    * 采取的策略是：deleted修改为true(就不会显示出来了)*/
    public BaseRespVo delete(@RequestBody GrouponRules grouponRules) {
        grouponService.deleteGrouponRules(grouponRules.getId());
        return BaseRespVo.ok();
    }


    @RequestMapping("listRecord")
    public BaseRespVo listRecord(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit,
                                 @RequestParam(name = "goodsId",required = false) Integer goodsId, @RequestParam("sort") String sort,
                                 @RequestParam("order") String order) {
        GrouponListRecordVO grouponListRecordVO =  grouponService.getGrouponListRecord(page,limit,goodsId,sort,order);
        return BaseRespVo.ok(grouponListRecordVO);
    }
}
